package org.hgvc.webapp.ets.jsf.beans.admin;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.faces.component.UIParameter;
import javax.faces.event.ActionEvent;
import javax.faces.model.SelectItem;

import org.hgvc.webapp.ets.bo.Partner;
import org.hgvc.webapp.ets.bo.RCIBooking;
import org.hgvc.webapp.ets.bo.RCIBookingDetail;
import org.hgvc.webapp.ets.bo.RCIBulkBank;
import org.hgvc.webapp.ets.bo.RCIMember;
import org.hgvc.webapp.ets.constant.AdminConstant;
import org.hgvc.webapp.ets.constant.BookingConstant;
import org.hgvc.webapp.ets.constant.CommonConstant;
import org.hgvc.webapp.ets.spring.service.AdminRCIService;
import org.hgvc.webapp.ets.spring.service.BookingService;
import org.hgvc.webapp.ets.spring.service.PartnerService;
import org.jocsb.base.AbstractBean;
import org.jocsb.base.exception.BaseException;


public class AdminRCIBookingBean extends AbstractBean{
	public static final String BEAN_NAME="adminRCIBookingBean";
	
	private BookingService bookingService;
	private AdminRCIService adminRCIService;
	private PartnerService partnerService;
	
	private List<RCIBooking> rciBookingList;
	private List<RCIBookingDetail> rciBookingDetailList;
	private List<RCIMember> rciMemberList;
	private List<Partner> partnerList;
	private List<RCIBulkBank> rciBulkBankList;
	
	private RCIBooking rciBooking;
	private RCIBookingDetail rciBookingDetail;
	private RCIMember rciMember;
	private Partner partner;
	private RCIBulkBank rciBulkBank;
	
	private List<SelectItem> selectRCIMember;
	private List<SelectItem> selectPartner;
	
	private boolean catchError;
	private boolean lookupForm;
	private boolean lookupPartnerForm;
	private boolean lookupRCIBulkBankForm;
	private List<SelectItem> selectSearchList;
	private String searchField;
	private String searchKeyword;
	
	public void setAdminRCIService(AdminRCIService adminRCIService) {
		this.adminRCIService = adminRCIService;
	}
	public void setPartnerService(PartnerService partnerService) {
		this.partnerService = partnerService;
	}
	public RCIBooking getRciBooking() {
		return rciBooking;
	}
	public void setRciBooking(RCIBooking rciBooking) {
		this.rciBooking = rciBooking;
	}
	public RCIBookingDetail getRciBookingDetail() {
		return rciBookingDetail;
	}
	public void setRciBookingDetail(RCIBookingDetail rciBookingDetail) {
		this.rciBookingDetail = rciBookingDetail;
	}
	public RCIMember getRciMember() {
		return rciMember;
	}
	public void setRciMember(RCIMember rciMember) {
		this.rciMember = rciMember;
	}
	public Partner getPartner() {
		return partner;
	}
	public void setPartner(Partner partner) {
		this.partner = partner;
	}
	public RCIBulkBank getRciBulkBank() {
		return rciBulkBank;
	}
	public void setRciBulkBank(RCIBulkBank rciBulkBank) {
		this.rciBulkBank = rciBulkBank;
	}
	public List<RCIBooking> getRciBookingList() {
		return rciBookingList;
	}
	public List<RCIBookingDetail> getRciBookingDetailList() {
		return rciBookingDetailList;
	}
	public List<RCIMember> getRciMemberList() {
		return rciMemberList;
	}
	public List<Partner> getPartnerList() {
		return partnerList;
	}
	public List<RCIBulkBank> getRciBulkBankList() {
		return rciBulkBankList;
	}
	public List<SelectItem> getSelectRCIMember() {
		return selectRCIMember;
	}
	public List<SelectItem> getSelectPartner() {
		return selectPartner;
	}
	public void setBookingService(BookingService bookingService) {
		this.bookingService = bookingService;
	}
	public boolean isLookupForm() {
		return lookupForm;
	}
	public void setLookupForm(boolean lookupForm) {
		this.lookupForm = lookupForm;
	}
	public String getSearchField() {
		return searchField;
	}
	public void setSearchField(String searchField) {
		this.searchField = searchField;
	}
	public String getSearchKeyword() {
		return searchKeyword;
	}
	public void setSearchKeyword(String searchKeyword) {
		this.searchKeyword = searchKeyword;
	}
	public List<SelectItem> getSelectSearchList() {
		return selectSearchList;
	}
	
	public String initRCIBookingAction(){
		String outcome=AdminConstant.NAV_STATUS_SUCCESS.concat("_"+getRoleName());
		try{
			this.rciBookingList=this.bookingService.getRCIBookingList();
			
			this.searchField=BookingConstant.SEARCH_BOOKING_NO;
			this.selectSearchList=new ArrayList<SelectItem>();
			SelectItem selectItem1=new SelectItem(BookingConstant.SEARCH_BOOKING_NO,BookingConstant.SEARCH_BOOKING_NO);
			this.selectSearchList.add(selectItem1);
			SelectItem selectItem2=new SelectItem(BookingConstant.SEARCH_CONFIRMATION_NO,BookingConstant.SEARCH_CONFIRMATION_NO);
			this.selectSearchList.add(selectItem2);
			
			
			this.rciBookingDetailList=new ArrayList<RCIBookingDetail>();
			this.rciMemberList=this.adminRCIService.getRCIMemberList();
			this.partnerList=this.partnerService.getApprovedPartnerList();
			this.rciBulkBankList=this.adminRCIService.getRCIBulkBankList();
			
			this.rciBooking=new RCIBooking();
			this.rciBookingDetail=new RCIBookingDetail();
			this.rciMember=new RCIMember();
			this.partner=new Partner();
			this.rciBulkBank=new RCIBulkBank();
			
		}catch(BaseException be){
			initErrorBean(be);
		}
		return outcome;
	}
	
	public String navAddRCIBookingAction(){
		String outcome=AdminConstant.NAV_STATUS_SUCCESS.concat("_"+getRoleName());
		if (this.catchError)
			outcome=null;
		return outcome;
	}
	public void navAddRCIBookingActionEvent(ActionEvent event){
		this.rciBooking=new RCIBooking();
		this.rciBookingDetailList=new ArrayList<RCIBookingDetail>();
	}
	public String addRCIBookingAction(){
		String outcome=AdminConstant.NAV_STATUS_SUCCESS.concat("_"+getRoleName());
		if (this.catchError)
			outcome=null;
		return outcome;
	}
	public void addRCIBookingActionEvent(ActionEvent event){
		this.catchError=false;
		try{
			this.rciBooking.setRciMember(this.rciMember);
			this.rciBooking.setPartner(this.partner);
			this.rciBooking.setCreatedBy(getThisUser());
			this.rciBooking.setCreatedDate(new Date());
			this.rciBooking.setIsDeleted(CommonConstant.NOT_DELETED);
			if (this.rciBooking.getConfirmationNo().length()>0 && this.rciBooking.getConfirmationDate()!=null)
				this.rciBooking.setStatus(BookingConstant.STATUS_CONFIRM);
			else
				this.rciBooking.setStatus(BookingConstant.STATUS_PENDING);
			this.bookingService.saveRCIBooking(this.rciBooking);
			this.rciBookingList=this.bookingService.getRCIBookingList();
		}catch(BaseException be){
			this.catchError=true;
			initErrorBean(be);
		}
	}
	
	public String navEditRCIBookingAction(){
		String outcome=AdminConstant.NAV_STATUS_SUCCESS.concat("_"+getRoleName());
		if (this.catchError)
			outcome=null;
		return outcome;
	}
	public void navEditRCIBookingActionEvent(ActionEvent event){
		this.catchError=false;
		UIParameter param = (UIParameter)event.getComponent().findComponent("rciBookingId");
		try{
			String rciBookingId=param.getValue().toString();
			for(RCIBooking rciBookingP : this.rciBookingList){
				if (rciBookingP.getRciBookingId().equalsIgnoreCase(rciBookingId)){
					this.rciBooking=rciBookingP;
					this.rciBookingDetailList=this.bookingService.getRCIBookingDetailList(this.rciBooking);
				}
			}	
			this.partner=this.rciBooking.getPartner();
		}catch(BaseException be){
			this.catchError=true;
			initErrorBean(be);
		}
	}
	
	public String updateRCIBookingAction(){
		String outcome=AdminConstant.NAV_STATUS_SUCCESS.concat("_"+getRoleName());
		if (this.catchError)
			outcome=null;
		return outcome;
	}
	public void updateRCIBookingActionEvent(ActionEvent event){
		this.catchError=false;
		try{
			this.rciBooking.setModifiedBy(getThisUser());
			this.rciBooking.setModifiedDate(new Date());
			this.bookingService.updateRCIBooking(this.rciBooking);
			this.rciBookingList=this.bookingService.getRCIBookingList();
		}catch(BaseException be){
			this.catchError=true;
			initErrorBean(be);
		}
	}
	
	public String navAddRCIBookingDetailAction(){
		String outcome=AdminConstant.NAV_STATUS_SUCCESS.concat("_"+getRoleName());
		if (this.catchError)
			outcome=null;
		return outcome;
	}
	public void navAddRCIBookingDetailActionEvent(ActionEvent event){
		this.rciBookingDetail=new RCIBookingDetail();
	}
	public String addRCIBookingDetailAction(){
		String outcome=AdminConstant.NAV_STATUS_SUCCESS.concat("_"+getRoleName());
		if (this.catchError)
			outcome=null;
		return outcome;
	}
	public void addRCIBookingDetailActionEvent(ActionEvent event){
		this.catchError=false;
		try{
			this.rciBookingDetail.setRciBooking(this.rciBooking);
			this.rciBookingDetail.setCreatedBy(getThisUser());
			this.rciBookingDetail.setCreatedDate(new Date());
			this.rciBookingDetail.setIsDeleted(CommonConstant.NOT_DELETED);
			this.bookingService.saveRCIBookingDetail(this.rciBookingDetail);
		}catch(BaseException be){
			this.catchError=true;
			initErrorBean(be);
		}
	}
	
	public String removeRCIBookingDetailAction(){
		String outcome=AdminConstant.NAV_STATUS_SUCCESS.concat("_"+getRoleName());
		if (this.catchError)
			outcome=null;
		return outcome;
	}
	public void removeRCIBookingDetailActionEvent(ActionEvent event){
		this.catchError=false;
		try{
			List<RCIBookingDetail> deleteList=new ArrayList<RCIBookingDetail>();
			for(RCIBookingDetail rciBookingDetailP : this.rciBookingDetailList){
				if (rciBookingDetailP.isMarkAsSelect())
					deleteList.add(rciBookingDetailP);
			}
			this.bookingService.deleteRCIBookingDetail(deleteList);
			this.rciBookingDetailList=this.bookingService.getRCIBookingDetailList(this.rciBooking);
		}catch(BaseException be){
			this.catchError=true;
			initErrorBean(be);
		}
	}

	public void lookupRCIMemberFormActionEvent(ActionEvent event){
		this.lookupForm=true;
		this.lookupPartnerForm=false;
		try{
			this.rciMemberList=this.adminRCIService.getRCIMemberList();	
		}catch(BaseException be){
			initErrorBean(be);
		}
	}
	public void lookupRCIMemberFormSelectedActionEvent(ActionEvent event){
		this.lookupForm=false;
		this.lookupPartnerForm=false;
		UIParameter param = (UIParameter)event.getComponent().findComponent("rciMemberId");
		String rciMemberId = param.getValue().toString();
		for(RCIMember rciMemberP : this.rciMemberList){
			if (rciMemberP.getRciMemberId().equalsIgnoreCase(rciMemberId)){
				this.rciMember=rciMemberP;
				break;
			}
		}
	}
	public void lookupRCIMemberFormCloseActionEvent(ActionEvent event){
		this.lookupForm=false;
		this.lookupPartnerForm=false;
		this.lookupRCIBulkBankForm=false;
	}
	
	public String searchRCIBookingAction(){
		String outcome=AdminConstant.NAV_STATUS_SUCCESS.concat("_"+getRoleName());
		if (this.catchError)
			outcome=null;
		return outcome;
	}
	public void searchRCIBookingActionEvent(ActionEvent event){
		
	}
	public boolean isLookupPartnerForm() {
		return lookupPartnerForm;
	}
	
	public void lookupPartnerFormActionEvent(ActionEvent event){
		this.lookupPartnerForm=true;
		this.lookupForm=false;
		this.lookupRCIBulkBankForm=false;
		try{
			this.partnerList=this.partnerService.getApprovedPartnerList();	
		}catch(BaseException be){
			initErrorBean(be);
		}
	}
	public void lookupPartnerFormSelectedActionEvent(ActionEvent event){
		this.lookupPartnerForm=false;
		this.lookupForm=false;
		this.lookupRCIBulkBankForm=false;
		UIParameter param = (UIParameter)event.getComponent().findComponent("partnerId");
		String partnerId = param.getValue().toString();
		for(Partner partnerP : this.partnerList){
			if (partnerP.getPartnerId().equalsIgnoreCase(partnerId)){
				this.partner=partnerP;
				break;
			}
		}
	}
	public void lookupPartnerFormCloseActionEvent(ActionEvent event){
		this.lookupPartnerForm=false;
		this.lookupForm=false;
		this.lookupRCIBulkBankForm=false;
	}
	public boolean isLookupRCIBulkBankForm() {
		return lookupRCIBulkBankForm;
	}
	
	
}
